package com.sl.hx.shop.ui.home.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.bean.HotList;
import com.sl.hx.shop.ui.home.adapter.BaseFragmentAdapter;
import com.sl.hx.shop.ui.home.contacts.HotListContacts;
import com.sl.hx.shop.ui.home.fragment.HotListFragment;
import com.sl.hx.shop.ui.home.presenter.HotListPresenter;
import com.sl.hx.shop.utils.MyUtils;
import com.sl.hx.shop.widget.TablayoutLineWidth;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 热销排行
 */
public class HotListActivity extends BaseActivity<HotListContacts.view,HotListPresenter> implements HotListContacts.view {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tablayout)
    XTabLayout tabLayout;
    @BindView(R.id.viewpage)
    ViewPager viewpager;

    private BaseFragmentAdapter mFragmentAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_hotlist;
    }

    @Override
    protected HotListPresenter createPresenter() {
        return new HotListPresenter(mContext);
    }

    @Override
    protected void initView() {
        mPresenter.getHotList();
    }



    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_back, R.id.tablayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tablayout:
                break;
        }
    }

    @Override
    public void ShowHotLists(List<HotList> hotLists) {
        initTablayout(hotLists);
    }

    private void initTablayout(List<HotList> hotLists) {
        List<String> mNameList = new ArrayList<>();
        List<Integer> mIdList = new ArrayList<>();
        List<Fragment> mFragmentList = new ArrayList<>();
        for (int i = 0; i < hotLists.size(); i++) {
            mNameList.add(hotLists.get(i).getName());
            mIdList.add(hotLists.get(i).getId());
            mFragmentList.add(createListFragments(hotLists.get(i)));
        }
        if(mFragmentAdapter==null) {
            mFragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager(), mFragmentList,mNameList);
        }else{
            //刷新fragment
            mFragmentAdapter.setFragments(getSupportFragmentManager(),mFragmentList,mNameList);
        }
        viewpager.setAdapter(mFragmentAdapter);
        tabLayout.setupWithViewPager(viewpager);
        viewpager.setOffscreenPageLimit(mNameList.size());
        MyUtils.dynamicSetTabLayoutMode(tabLayout);

    }

    private HotListFragment createListFragments(HotList hotList) {
        HotListFragment fragment=new HotListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("hotlistId",hotList.getId());
        bundle.putString("hotlistKey",hotList.getKey());
        fragment.setArguments(bundle);
        return fragment;
    }
}
